package com.game.utils  
{
	
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * 
	 * @author chery
	 */
	public class SetMcGoto_bak 
	{
		private var _mc:MovieClip
		private var _minNum:Number
		private var _maxNum:Number
		private var _nowNum:Number
		private var _currFrame:uint
		private var _gotoFrame:uint
		private var _type:int
		private var _addFrame:Number
		private var _nowAddFrame:Number
		private var _totalNum:Number
		private var _totalFrames:uint
		private var _endFun:Function
		
		/**
		 * 让影片剪辑根据数值播放到制定帧
		 * @param frameRate 帧频
		 * @param type  type=0:无过程,type=1,有过程,type=2,按时间
		 * @param mc  设置影片剪辑
		 * @param nowNum 当前数值
		 * @param minNum 最小值
		 * @param maxNum 最大值
		 * @param endFun 播放到制定帧时调用
		 */
		public function SetMcGoto_bak(frameRate:uint,type:int,mc:MovieClip,nowNum:Number=0,minNum:Number=0,maxNum:Number=0,endFun:Function=null)
		{
			
			
			_mc=mc 
			_nowNum=nowNum
			_minNum=minNum
			_maxNum=maxNum
			_type=type
			_totalNum=Math.abs(maxNum)+Math.abs(minNum)
			_currFrame=_mc.currentFrame
			_totalFrames=_mc.totalFrames
			_endFun=endFun
			if(_type!=2){
				setNum(_nowNum)
			}else{
				_nowAddFrame=0
				_addFrame=(_totalFrames/frameRate)/_nowNum
				
				setTimeGoto()
			}
		}
		/**
		 * 修改当前数值
		 * @param nowNum 新数值
		 */
		public function setNum(nowNum:Number):void{
			_nowNum=nowNum
			_gotoFrame=getFrame()
			
			
			setGoto(_gotoFrame,_type)
		}
		private function setTimeGoto():void{
			_mc.addEventListener(Event.ENTER_FRAME,onSetTimeGoto)
			
		}
		private function onSetTimeGoto(E:Event):void{
			_nowAddFrame+=_addFrame
			_currFrame=Math.floor(_nowAddFrame)			
			
			_mc.gotoAndStop(_currFrame)
			if(_currFrame==_totalFrames){
				_mc.removeEventListener(Event.ENTER_FRAME,onSetTimeGoto)
				_endFun()
			}
		}
		
		
		private function setGoto(fNum:uint,type:int):void{
			if(!type){
				_mc.gotoAndStop(fNum)
				this._currFrame=fNum
			}else{
				_mc.addEventListener(Event.ENTER_FRAME,onEnterFrame)
			}
		}
		private function onEnterFrame(E:Event):void{
			
			if(_gotoFrame>_currFrame){
				_mc.gotoAndStop(_currFrame++);
				
				
			}else if(_gotoFrame<_currFrame){
				_mc.gotoAndStop(_currFrame--);
				
			}else{
				
				_mc.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				_currFrame=_gotoFrame;
				_mc.gotoAndStop(_gotoFrame);
			}
			
			
		}
		private function getFrame():uint{
			
			var  thisGoto:uint=Math.ceil(_totalFrames*(_nowNum/_totalNum))
			
			if(_maxNum<_minNum){
				
				thisGoto=_totalFrames-thisGoto
				
			}
			if(thisGoto<=0){
				thisGoto=1
			}
			return thisGoto
		}
	}
}